package com.cetc.sdp.kmga.cs.util;

import java.io.IOException;
import java.io.InputStream;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import java.util.StringTokenizer;

/**
 * @description:
 * @author： DengQiang
 * @date: 2017/8/17 16:10
 */
public class Tool {

    public final static DateTimeFormatter DEFAULT_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public final static DateTimeFormatter MILL_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    public final static ZoneId zoneId = ZoneId.of("Asia/Shanghai");

    /**
     * 将时间戳字符串格式化为日期字符串
     *
     * @param timestamp 时间戳，秒
     * @return
     */
    public static String timestampToDate(String timestamp) {
        if (timestamp == null || timestamp.isEmpty() || timestamp.equals("0")) {
            return "";
        }
        long stamp = Long.parseLong(timestamp);
        Instant instant = Instant.ofEpochSecond(stamp);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        return localDateTime.format(DEFAULT_TIME_FORMATTER);
    }

    /**
     * 格式化时间字符串
     *
     * @param timeStr
     * @return
     */
    public static LocalDateTime getTimeFromOrDefault(String timeStr) {
        try {
            if (timeStr != null) {
                return LocalDateTime.from(Tool.DEFAULT_TIME_FORMATTER.parse(timeStr));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        LocalDateTime now = LocalDateTime.now();
        return now.minusNanos(now.getNano());
    }

    /**
     * 时间戳字符串转日期
     *
     * @param timestamp milliseconds
     * @return
     */
    public static LocalDateTime timestampToLDT(String timestamp) {
        if (timestamp == null || timestamp.isEmpty() || timestamp.equals("0")) {
            return null;
        }
        long stamp = Long.parseLong(timestamp);
        return timestampToLDT(stamp);
    }

    /**
     * 时间戳转日期
     *
     * @param milliseconds
     * @return
     */
    public static LocalDateTime timestampToLDT(long milliseconds) {
        Instant instant = Instant.ofEpochSecond(milliseconds);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        return localDateTime;
    }

    /**
     * 日期字符串转时间戳
     *
     * @param date
     * @return
     */
    public static long dateToTimestamp(String date) {
        return LocalDateTime.from(DEFAULT_TIME_FORMATTER.parse(date)).atZone(zoneId).toInstant().toEpochMilli();
    }

    /**
     * 日期转时间戳
     *
     * @param date
     * @return
     */
    public static long dateToTimestamp(LocalDateTime date) {
        return date.atZone(zoneId).toInstant().toEpochMilli();
    }

    public static Properties loadProperties(String path) {
        Properties prop = new Properties();
        InputStream is = Tool.class.getClassLoader().getResourceAsStream(path);
        try {
            prop.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prop;
    }

    public static String getLongIpToString(Long ipLong) {
        if (ipLong == null || ipLong == 0) return null;
        StringBuilder sb = new StringBuilder();
        sb.append(ipLong >>> 24);
        sb.append('.');
        sb.append(String.valueOf((ipLong & 0x00FFFFFF) >>> 16));
        sb.append('.');
        sb.append(String.valueOf((ipLong & 0x0000FFFF) >>> 8));
        sb.append('.');
        sb.append(String.valueOf(ipLong & 0x000000FF));
        return sb.toString();
    }

    public static Long getStringIpToLong(String ipString) {
        if (ipString == null || ipString.isEmpty()) return 0L;
        long result = 0L;
        StringTokenizer token = new StringTokenizer(ipString, ".");
        result += Long.parseLong(token.nextToken()) << 24;
        result += Long.parseLong(token.nextToken()) << 16;
        result += Long.parseLong(token.nextToken()) << 8;
        result += Long.parseLong(token.nextToken());
        return result;
    }

    public static DayOfWeek formatWeek(String str) {
        if (str == null) {
            return null;
        }
        switch (str) {
            case "1":
                return DayOfWeek.MONDAY;
            case "2":
                return DayOfWeek.TUESDAY;
            case "3":
                return DayOfWeek.WEDNESDAY;
            case "4":
                return DayOfWeek.THURSDAY;
            case "5":
                return DayOfWeek.FRIDAY;
            case "6":
                return DayOfWeek.SATURDAY;
            case "7":
                return DayOfWeek.SUNDAY;
            default:
                return null;
        }
    }
}
